package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.poi.ExcelUtil;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "获取分类树")
    @GetMapping("/treeSelect/{parentId}")
    public AjaxResult getCategoryTree(@PathVariable Long parentId) {
        //调用CategoryService中根据父id获取分类数据的方法
        List<Category> categoryList = categoryService.getCategoryTree(parentId);
        return AjaxResult.success(categoryList);
    }

    @Operation(summary = "将所有的分类数据导出到Excel文件中")
    @PostMapping("/export")
    public void exportCategoryToExcel(HttpServletResponse response, Category category) {
        //获取所有的分类数据
        List<Category> categoryList = categoryService.list();
        //创建ExcelUtil对象
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, categoryList, "分类数据");
    }

    @Operation(summary = "导入分类数据时下载Excel模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.importTemplateExcel(response, "分类数据");
    }

    @Operation(summary = "将Excel中的数据导入到数据库中")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        //创建ExcelUtil对象
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        //导入Excel文件
        List<Category> categoryList = util.importExcel(file.getInputStream());
        //获取登录的用户名
        String operName = SecurityUtils.getUsername();
        //导入Excel中的数据
        String message = categoryService.importCategory(categoryList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @InnerAuth
    @Operation(summary = "获取商品一级分类")
    @GetMapping(value = "/getOneCategory")
    public R<List<CategoryVo> > getOneCategory() {
        return R.ok(categoryService.getOneCategory());
    }

    @InnerAuth
    @Operation(summary = "微服务内部远程调用获取一级分类的接口")
    @GetMapping("/getFirstLevelCategory/{parentId}")
    public R<List<CategoryVo>> getFirstLevelCategory(@PathVariable Long parentId) {
        //调用当前类中获取分类树的方法
        AjaxResult categoryTree = getCategoryTree(parentId);
        List<Category> categoryList = (List<Category>) categoryTree.get("data");
        //使用stream流将List<Category>转换为List<CategoryVo>集合
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            //复制属性值
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return R.ok(categoryVoList);
    }

    @InnerAuth
    @Operation(summary = "获取分类嵌套列表")
    @GetMapping(value = "/tree")
    public R<List<CategoryVo> > tree() {
        return R.ok(categoryService.tree());
    }

}


